package cn.stylefeng.guns.modular.yingbao.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.yingbao.entity.Payee;
import cn.stylefeng.guns.modular.yingbao.entity.UserBank;
import cn.stylefeng.guns.modular.yingbao.mapper.PayeeMapper;
import cn.stylefeng.guns.modular.yingbao.mapper.UserBankMapper;
import cn.stylefeng.guns.modular.yingbao.model.params.PayeeParam;
import cn.stylefeng.guns.modular.yingbao.model.result.PayeeResult;
import  cn.stylefeng.guns.modular.yingbao.service.PayeeService;
import cn.stylefeng.guns.modular.yingbao.service.UserBankService;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 平台收款信息 服务实现类
 * </p>
 *
 * @author zs
 * @since 2019-12-19
 */
@Service
public class PayeeServiceImpl extends ServiceImpl<PayeeMapper, Payee> implements PayeeService {

    @Resource
    UserBankMapper userBankMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(PayeeParam param){
        Payee entity = getEntity(param);
        this.save(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(PayeeParam param){
        this.removeById(getKey(param));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(PayeeParam param){
        Payee oldEntity = getOldEntity(param);
        Payee newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public PayeeResult findBySpec(PayeeParam param){
        return null;
    }

    @Override
    public List<PayeeResult> findListBySpec(PayeeParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(PayeeParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public ResponseData queryByNameAndId(String id) {
        Payee nameid = this.baseMapper.selectOne(new QueryWrapper<Payee>().eq("payid", id));
        if(nameid.getPayid() == 3){
            UserBank userBank = userBankMapper.selectById(nameid.getPhone());
            if(userBank == null){
                return ResponseData.error("改银行卡已经被消除");
            }
            nameid.setName(userBank.getName());
        }
        if(nameid != null){
            return ResponseData.success(nameid);
        }
        return ResponseData.error(500,"暂未上传此种开通方式");
    }

    @Override
    public Integer upById(Payee byId) {
        Integer x = baseMapper.upById(byId);
        return x;
    }

    @Override
    public List<Payee> queryAll() {
        return this.baseMapper.selectList(new QueryWrapper<Payee>().eq("backup1", "0"));
        /*if(backup1.size() > 0){
            return ResponseData.success(backup1);
        }
        return ResponseData.error(500,"暂无平台收款信息");*/
    }

    @Override
    public List<Payee> queryAll1() {
        List<Payee> backup1 = this.baseMapper.selectList(new QueryWrapper<Payee>().eq("backup1", "0"));
        if(backup1.size() > 0){
            backup1.forEach( m -> {
                if(m.getPayid() == 3){
                    UserBank id = userBankMapper.selectOne(new QueryWrapper<UserBank>().eq("id", m.getPhone()));
                    m.setBackup1(id.getName());
                }
            });
            return backup1;
        }
        return null;
    }

    private Serializable getKey(PayeeParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Payee getOldEntity(PayeeParam param) {
        return this.getById(getKey(param));
    }

    private Payee getEntity(PayeeParam param) {
        Payee entity = new Payee();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
